import Vue from "vue"
let prototype = Vue.prototype;

export default {
    /**
     * 服务分类
     */
    getCate(data) {
        return new Promise((resolve, reject) => {
            prototype.$request({
                api: "service.cate",
                data: data
            }).then((response) => {
                if (response.code == 1) {
                    let list = [];
                    response.data.forEach((item) => {
                        let child = [];
                        item.child.forEach((v) => {
                            child.push({
                                id: v.id,
                                name: v.name,
                                cover: v.more,
                                page: "",
                            });
                        });
                        list.push({
                            id: item.id,
                            name: item.name,
                            cover: item.more,
                            child: child,
                            page: "",
                        });
                    });
                    return resolve(list);
                }
                return reject(response.msg);
            });
        });
    },
    /**
     * 保险价格
     */
    getInsurancePrice() {
        return new Promise((resolve, reject) => {
            prototype.$request({
                api: "service.insurance",
            }).then((response) => {
                if (response.code == 1) {
                    let money = parseFloat(response.data.money);
                    resolve(money > 0 ? money : 0)
                }
            }).catch(e => { });
        });
    },
    /**
     * 服务详情
     */
    getServiceDetail(id) {
        return new Promise((resolve, reject) => {
            prototype.$request({
                api: "service.detail",
                data: {
                    id: id,
                },
            }).then((response) => {
                if (response.code == 1) {
                    return resolve(response.data);
                }
                return reject(response.msg);
            }).catch(e => { });
        })
    },
    /**
     * 工程提交
     */
    projectApply(data) {
        return new Promise((resolve, reject) => {
            prototype.$request({
                api: "service.project",
                data: data
            }).then(response => {
                if (response.code == 1) {
                    return resolve(response.msg)
                }
                return reject(response.msg);
            }).catch(e => { });
        });
    },
    /**
     * 企业提交
     */
    businessApply(data) {
        return new Promise((resolve, reject) => {
            prototype.$request({
                api: "service.business",
                data: data
            }).then(response => {
                if (response.code == 1) {
                    return resolve(response.msg)
                }
                return reject(response.msg);
            }).catch(e => { });
        });
    },
    /**
     * 联保提交
     */
    afterMarketApply(data) {
        return new Promise((resolve, reject) => {
            prototype.$request({
                api: "service.afterMarket",
                data: data
            }).then(response => {
                if (response.code == 1) {
                    return resolve(response.msg)
                }
                return reject(response.msg);
            }).catch(e => { });
        });
    },
    /**
     * 创建需求订单
     */
    createDemandOrder(data) {
        return new Promise((resolve, reject) => {
            prototype.$request({
                api: "order.create.t2",
                data: data,
            }).then(response => {
                if (response.code == 1) {
                    return resolve(response);
                }
                return reject(response.msg);
            }).catch(e => { });
        });
    },
    /**
     * 获取货运配置
     */
    distributionConfig() {
        return new Promise((resolve, reject) => {
            prototype.$request({
                api: "service.distribution.config",
            }).then(response => {
                if (response.code == 1) {
                    return resolve({
                        unitPrice: response.data.money,
                        minDistance: response.data.min,
                    });
                }
                return reject(response.msg);
            }).catch(e => { });
        });
    },
    distributionCar() {
        return new Promise((resolve, reject) => {
            prototype.$request({
                api: "service.distribution.carType",
            }).then(response => {
                if (response.code == 1) {
                    let list = [];
                    response.data.forEach(item => {
                        list.push({
                            id: item.id,
                            name: item.title
                        });
                    });
                    return resolve(list);
                }
                return reject(response.msg);
            }).catch(e => { });
        });
    },
    /**
     * 创建货运订单
     */
    createDistributionOrder(data) {
        return new Promise((resolve, reject) => {
            prototype.$request({
                api: "order.create.t3",
                data: data,
            }).then(response => {
                if (response.code == 1) {
                    return resolve(response.data);
                }
                return reject(response.msg);
            }).catch(e => { });
        });
    },
}
